<?php
$areas = ['erqiqu','zhongyuanqu1','guanchenghuizuqu','huijiqu','jinshuiqu','zhengdongxinqu'];
$page = 2;
$connect = [
    'host'     => '47.100.13.53',
    'port'     => 3306,
    'user'     => 'seckill',
    'password' => '123456',
    'database' => 'seckill'
];

$mysql = new Swoole\Coroutine\MySQL();

$count = 0;
foreach ($areas as $key => $area) {
    $pro = new Swoole\Process(function ($worker) use ($area, $page) {
        for ($i = 1; $i < $page; $i++) {
            $url = "https://zz.lianjia.com/zufang/{$area}/pg{$i}";
            $json = getData($url);
            foreach ($json as $k => $v) {
                $worker->push(json_encode($v,JSON_UNESCAPED_UNICODE));
            }
        }
    });
    $pro->useQueue();
    $pid = $pro->start();
    $process[$pid] = $pro;
}

foreach ($process as $key => $worker) {
     for($i=1;$i<=$page*60;$i++){
         $data = $worker->pop();
          insert_data(json_decode($data,true),$connect,$mysql);
      }
}

function insert_data($v,$connect,$mysql)
{
   $sch = new \Swoole\Coroutine\Scheduler();
   $sch->add(function () use ($v,$connect,$mysql){
       $mysql->connect($connect);
       $stmt = $mysql->prepare('INSERT INTO info (title,address,square,direction,huxing,price)VALUES(?,?,?,?,?,?)');
       $stmt->execute([$v['title'],$v['address'],$v['square'],$v['direction'],$v['huxing'],$v['price']]);
   });
   $sch->start();
}


function getData($url)
{
    $html = curl_data($url);
    $datas = [];
    $preg_div = '/<div class=\"content__list--item--main\">.*?<\/div>/ism';
    preg_match_all($preg_div,$html,$match_div);
    foreach ($match_div[0] as $k => $v) {
        //过滤标题
        $preg_a = '/<a .*?>.*?<\/a>/ism';
        preg_match_all($preg_a,$v,$match_a);
        //过滤 平方，朝向和户型
        $preg_i = '/<\/i>.*?<i>/ism';
        preg_match_all($preg_i,$v,$match_i);
        //过滤 价位
        $preg_em = '/<em>.*?<\/em>/ism';
        preg_match_all($preg_em,$v,$match_em);
        $datas[$k]['title'] = trim(strip_tags($match_a[0][0]));
        $datas[$k]['address'] = (isset($match_a[0][1])?trim(strip_tags($match_a[0][1])):"").(isset($match_a[0][2])?trim(strip_tags($match_a[0][2])):"").(isset($match_a[0][3])?trim(strip_tags($match_a[0][3])):"无");
        $datas[$k]['square'] = trim(strip_tags($match_i[0][0]));
        $datas[$k]['direction'] = isset($match_i[0][1])?trim(strip_tags($match_i[0][1])):"";
        $datas[$k]['huxing'] = isset($match_i[0][2])?trim(strip_tags($match_i[0][2])):"";
        $datas[$k]['price'] = trim(strip_tags($match_em[0][0]));
    }
    return $datas;
}


function curl_data($url)
{
// 模拟谷歌浏览器的请求头
    $headers = [
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection: keep-alive'
    ];

// Cookie 数据，需要替换为真实有效的 Cookie
    $cookies = [
        'lianjia_ssid' => '010c23a5-c086-4d06-9f0a-4284ee187750',
        'lianjia_uuid' => 'fcb087f7-c42c-4ebb-9a06-067d5d18a48a',
        'select_city' => '410100'
    ];

// 构建 Cookie 字符串
    $cookieString = '';
    foreach ($cookies as $name => $value) {
        $cookieString .= urlencode($name) . '=' . urlencode($value) . '; ';
    }
    $cookieString = rtrim($cookieString, '; ');

// 初始化 cURL
    $ch = curl_init();

// 设置 cURL 选项
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_COOKIE, $cookieString);

// 执行请求
    $response = curl_exec($ch);

// 检查是否有错误
    if (curl_errno($ch)) {
        echo 'Curl error: ' . curl_error($ch);
    }

// 关闭 cURL
    curl_close($ch);

// 输出响应
    return $response;
}